package com.seg.system.service.impl;

import com.seg.common.exception.ServiceException;
import com.seg.common.utils.DateUtils;
import com.seg.common.utils.StringUtils;
import com.seg.common.utils.bean.BeanValidators;
import com.seg.system.SelectDataI;
import com.seg.system.domain.SelectData;
import com.seg.system.domain.TEquipment;
import com.seg.system.domain.TEquipmentV;
import com.seg.system.mapper.TEquipmentMapper;
import com.seg.system.service.ITEquipmentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2023-06-26
 */
@Service
public class TEquipmentServiceImpl implements ITEquipmentService {
    @Autowired
    private TEquipmentMapper tEquipmentMapper;
    @Autowired
    protected Validator validator;

    private static final Logger log = LoggerFactory.getLogger(TEquipmentServiceImpl.class);

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TEquipment selectTEquipmentById(Long id) {
        return tEquipmentMapper.selectTEquipmentById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tEquipment 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TEquipment> selectTEquipmentList(TEquipment tEquipment) {
        return tEquipmentMapper.selectTEquipmentList(tEquipment);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tEquipment 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTEquipment(TEquipment tEquipment) {
        tEquipment.setCreateTime(DateUtils.getNowDate());
        int i = tEquipmentMapper.selectTEquipmentBySerialNo(tEquipment.getSerialNo());
        if (i == 1) {
            return -1;
        }
        return tEquipmentMapper.insertTEquipment(tEquipment);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tEquipment 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTEquipment(TEquipment tEquipment) {
        tEquipment.setUpdateTime(DateUtils.getNowDate());
        return tEquipmentMapper.updateTEquipment(tEquipment);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTEquipmentByIds(Long[] ids) {
        return tEquipmentMapper.deleteTEquipmentByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTEquipmentById(Long id) {
        return tEquipmentMapper.deleteTEquipmentById(id);
    }

    @Override
    public String importData(List<TEquipmentV> itemList, boolean updateSupport, String operName) {
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        Date upDate = new Date();
        if (StringUtils.isNull(itemList) || itemList.size() == 0) {
            throw new ServiceException("导入清单数据不能为空！");
        }
        for (TEquipmentV equipment : itemList) {
            try {
                int t = tEquipmentMapper.selectTEquipmentBySerialNo(equipment.getSerialNo());
                if (t == 0) {
                    BeanValidators.validateWithException(validator, equipment);
                    TEquipment ep = new TEquipment();
                    ep.setSerialNo(equipment.getSerialNo());
                    ep.setEquipName(equipment.getEquipName());
                    ep.setLine(equipment.getLine());
                    ep.setStation(equipment.getStation());
                    ep.setQuantity(equipment.getQuantity());
                    ep.setUnit(equipment.getUnit());
                    ep.setSopTime(equipment.getSopTime());
                    ep.setRemark(equipment.getRemark());
                    if (!equipment.equals("使用")) {
                        ep.setStatus(1L);
                    } else {
                        ep.setStatus(0L);
                    }
                    ep.setCreateBy(operName);
                    ep.setDelFlag("0");
                    int i = tEquipmentMapper.insertTEquipment(ep);
                    if (i == 1) {
                        successNum++;
                        successMsg.append("<br/>" + successNum + "、清单条目" + equipment.getSerialNo() + " 导入成功");
                    } else {
                        failureNum++;
                        failureMsg.append("<br/>" + failureNum + "、清单条目 " + equipment.getSerialNo() + " 导入失败");
                    }
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、清单条目 " + equipment.getSerialNo() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、清单条目 " + equipment.getSerialNo() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<SelectData> getProductLine() {
        List<SelectData> list = new ArrayList<>();
        List<String> productLine = tEquipmentMapper.getProductLine();
        for (String line : productLine) {
            SelectData selectData = new SelectData();
            selectData.setName(line);
            selectData.setValue(line);
            list.add(selectData);
        }
        return list;
    }

    @Override
    public List<SelectData> getStation(String line) {
        List<SelectData> list = new ArrayList<>();
        List<String> station = tEquipmentMapper.getStation(line);
        for (String s : station) {
            SelectData selectData = new SelectData();
            // modify:修改逻辑，通过产线和工位获取设备名称
            String equipmentName = tEquipmentMapper.getEquipmentNameByLineAndStation(line,s);
            selectData.setName(s);
            selectData.setValue(equipmentName);
            list.add(selectData);
        }
        return list;
    }

    @Override
    public List<SelectDataI> getStatus() {
        List<SelectDataI> list = new ArrayList<>();
        List<Long> station = tEquipmentMapper.getStatus();
        for (Long s : station) {
            SelectDataI selectData = new SelectDataI();
            selectData.setName(formatStatus(s));
            selectData.setValue(s);
            list.add(selectData);
        }
        return list;
    }

    @Override
    public List<SelectData> getOutType() {
        List<SelectData> list = new ArrayList<>();
        List<String> typeList = Arrays.asList("A", "B", "C");
        for (String t : typeList) {
            SelectData selectData = new SelectData();
            selectData.setName(t);
            selectData.setValue(t);
            list.add(selectData);
        }
        return list;
    }

    private String formatStatus(Long s) {
        String str = "";
        if (s == 0L) {
            str = "未使用";
        } else if (s == 1L) {
            str = "使用";
        }
        return str;
    }
}
